package cn.ssdcc.dkserver.mapper;

import cn.ssdcc.dkserver.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("CREATE TABLE IF NOT EXISTS `thirddevice` (\n" +
            "\t`id` integer NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
            "\t`deviceid` varchar(100) NOT NULL COMMENT '主机id',\n" +
            "\t`thirdid` varchar(100) NOT NULL COMMENT '副机id',\n" +
            "\tPRIMARY KEY (`id`)\n" +
            ")")
    int createThirdDevice();

    @Insert("CREATE TABLE IF NOT EXISTS `user` (\n" +
            "\t`id` integer NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
            "\t`name` varchar(100) NOT NULL COMMENT '名',\n" +
            "\t`deviceid` varchar(100) NOT NULL COMMENT '机器码',\n" +
            "\t`img` varchar(100) NOT NULL COMMENT '头像',\n" +
            "\t`grade` varchar(100) NOT NULL COMMENT '等级',\n" +
            "\t`black` varchar(100) NOT NULL COMMENT '黑名单标志',\n" +
            "\tPRIMARY KEY (`id`)\n" +
            ")")
    int createUserTable();

    @Select("SELECT id, name, deviceid, img, grade, black\n" +
            "FROM `user` WHERE deviceid = #{deviceid};\n")
    List<User> selectUserByDeviceid(@Param("deviceid")String deviceid);

    @Select("SELECT deviceid \n" +
            "FROM thirddevice WHERE thirdid = #{deviceid};\n")
    List<String> selectUserDeviceidByOthertDeviceid(@Param("deviceid")String deviceid);

    @Insert("INSERT INTO `user`\n" +
            "(name, deviceid, img, grade, black)\n" +
            "VALUES('游客', #{deviceid}, '/static/Logo.png', '1', '0');\n")
    int insertNewUser(@Param("deviceid")String deviceid);

    @Update("UPDATE `user`\n" +
            "SET name=#{user.name}, img=#{user.img}\n" +
            "WHERE id= #{user.id} ;\n")
    int updateUser(@Param("user")User user);

    @Insert("INSERT INTO dk.thirddevice\n" +
            "(deviceid, thirdid)\n" +
            "VALUES(#{deviceid}, #{thirdid});")
    int insertThirddevice(@Param("deviceid")String deviceid,@Param("thirdid")String thirdid);

    @Delete("DELETE FROM dk.thirddevice\n" +
            "WHERE thirdid= #{deviceid} ;")
    int deleteThirddevice(@Param("deviceid")String deviceid);

}
